package com.mm.serivce.common.impl;

import cn.hutool.core.date.DateUtil;
import cn.hutool.core.util.StrUtil;
import cn.hutool.json.JSONArray;
import cn.hutool.json.JSONObject;
import cn.hutool.json.JSONUtil;
import com.mm.base.exception.MuMuException;
import com.mm.base.forest.ThirdPartyClient;
import com.mm.domain.other.*;
import com.mm.serivce.common.ThirdPartyService;
import com.mm.util.common.PinyinUtil;
import com.mm.util.common.RedisUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * 描述:
 *
 * @author: yanglin
 * @Date: 2022-01-25-9:43
 * @Version: 1.0
 */
@Slf4j
@Service
public class ThirdPartyServiceImpl implements ThirdPartyService {

    private final ThirdPartyClient client;

    private final RedisUtil redisUtil;

    private final PinyinUtil pinyinUtil;

    public ThirdPartyServiceImpl(ThirdPartyClient client, RedisUtil redisUtil, PinyinUtil pinyinUtil) {
        this.client = client;
        this.redisUtil = redisUtil;
        this.pinyinUtil = pinyinUtil;
    }

    @Override
    public List<WeatherInfo> queryWeatherInfo(String cityName, Integer type) {
        assert StrUtil.isBlank(cityName) : "天气城市名不能为空!";
        if (type == null) {
            type = 1;
        }
        List<WeatherInfo> weatherInfos = new ArrayList<>();
        // 数据存储缓存；当天城市仅请求一次
        String pinyin = pinyinUtil.getStringPinYin(cityName);
        // 当天日期
        String dateStr = DateUtil.format(DateUtil.date(), "mmdd");
        String weatherKey = "weather:" + pinyin + ":" + dateStr;
        String weatherStr = "";
        try{
            if (redisUtil.hasKey(weatherKey)) {
                weatherStr = redisUtil.get(weatherKey).toString();
            }else{
                weatherStr = client.queryWeatherOf(cityName, type);
            }
            if (StrUtil.isNotBlank(weatherStr)) {
                JSONObject weather = JSONUtil.parseObj(weatherStr);
                String data = weather.get("data").toString();
                if (data.contains("[")){
                    JSONArray jsonArray = JSONUtil.parseArray(data);
                    jsonArray.forEach(json -> {
                        JSONObject weatherJson = JSONUtil.parseObj(json);
                        WeatherInfo weatherInfo = JSONUtil.toBean(weatherJson, WeatherInfo.class);
                        weatherInfos.add(weatherInfo);
                    });
                }else{
                    WeatherInfo weatherInfo = JSONUtil.toBean(data, WeatherInfo.class);
                    weatherInfos.add(weatherInfo);
                }
                redisUtil.set(weatherKey, weatherStr);
            }
        }catch (Exception e){
            e.printStackTrace();
            throw new MuMuException("查询城市天气Error!");
        }
        return weatherInfos;
    }

    @Override
    public List<Epidemic> queryEpidemic(String type) {
        if (type == null) {
            type = "epidemicInfectionData";
        }
        List<Epidemic> epidemicList = new ArrayList<>();
        // 当天日期
        String dateStr = DateUtil.format(DateUtil.date(), "mmdd");
        String epidemicKey = "epidemic:" + dateStr + ":" + type ;
        String epidemicStr = "";
        try{
            if (redisUtil.hasKey(epidemicKey)) {
                epidemicStr = redisUtil.get(epidemicKey).toString();
            }else{
                epidemicStr = client.queryEpidemic(type);
            }
            if (StrUtil.isNotBlank(epidemicStr)) {
                JSONObject epidemic = JSONUtil.parseObj(epidemicStr);
                String data = epidemic.get("data").toString();
                //[epidemicRiskLevel(高危等级地区)
                //epidemicInfectionData(疫情数据)
                //epidemicHotspot(疫情热点)]
                if ("epidemicRiskLevel".equals(type)){
                    JSONObject epidemicJson = JSONUtil.parseObj(data);
                    EpidemicHead epidemicHead = JSONUtil.toBean(epidemicJson, EpidemicHead.class);
                    epidemicList.add(epidemicHead);
                }else if ("epidemicInfectionData".equals(type)){

                }else if ("epidemicHotspot".equals(type)){
                    JSONArray jsonArray = JSONUtil.parseArray(data);
                    jsonArray.forEach(json -> {
                        JSONObject epidemicJson = JSONUtil.parseObj(json);
                        EpidemicHot epidemicInfo = JSONUtil.toBean(epidemicJson, EpidemicHot.class);
                        epidemicList.add(epidemicInfo);
                    });
                }
                redisUtil.set(epidemicKey, epidemicStr);
            }

        }catch (Exception e){
            e.printStackTrace();
            throw new MuMuException("查询疫情信息Error!");
        }
        return epidemicList;
    }

    @Override
    public ExpressInfo queryExpressInfo(String express) {
        assert StrUtil.isBlank(express) : "快递单号不能为空!";
        ExpressInfo expressInfo = new ExpressInfo();
        // 当天日期
        String dateStr = DateUtil.format(DateUtil.date(), "mmdd");
        String expressKey = "express:"+ express + ":" + dateStr;
        String expressStr = "";
        try{
            if (redisUtil.hasKey(expressKey)) {
                expressStr = redisUtil.get(expressKey).toString();
            }else{
                expressStr = client.queryExpressInfo(express);
            }
            if (StrUtil.isNotBlank(expressStr)){
                JSONObject epidemic = JSONUtil.parseObj(expressStr);
                String data = epidemic.get("data").toString();
                expressInfo = JSONUtil.toBean(data, ExpressInfo.class);
                redisUtil.set(expressKey, expressStr);
            }

        }catch (Exception e){
            e.printStackTrace();
            throw new MuMuException("查询快递信息Error!");
        }
        return expressInfo;
    }

    @Override
    public MobileAddress queryMobileAddress(String phone) {
        assert phone == null : "手机号码不能为空!";
        MobileAddress mobileAddress = new MobileAddress();
        String phoneKey = "phone:"+ phone;
        String phoneStr = "";
        try{
            if (redisUtil.hasKey(phoneKey)) {
                phoneStr = redisUtil.get(phoneKey).toString();
            }else{
                phoneStr = client.queryMobileAddress(phone);
            }
            if (StrUtil.isNotBlank(phoneStr)){
                JSONObject epidemic = JSONUtil.parseObj(phoneStr);
                String data = epidemic.get("data").toString();
                mobileAddress = JSONUtil.toBean(data, MobileAddress.class);
                redisUtil.set(phoneKey, phoneStr);
            }

        }catch (Exception e){
            throw new MuMuException("查询手机号归属地息Error!");
        }
        return mobileAddress;
    }
}
